﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
namespace NASM_Develop_IDE
{
   
    static class NASMBuilder
    {
        public static bool Build(string command, out StringBuilder buildOutput)
        {
            try
            {
                buildOutput = new StringBuilder();
                ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe");
                startInfo.Arguments = "/C " + " nasm " + command;
                startInfo.RedirectStandardError = true;
                startInfo.RedirectStandardOutput = true;
                startInfo.UseShellExecute = false;
                startInfo.CreateNoWindow = true;
                Process p = Process.Start(startInfo);
                string output = p.StandardOutput.ReadToEnd();
                string error = p.StandardError.ReadToEnd();
                p.WaitForExit();
                if (output.Length != 0)
                    buildOutput.Append(output);
                else if (error.Length != 0)
                    buildOutput.Append(error);
                else
                    buildOutput.Append("\n");
                return true;
            }
            catch
            {
                buildOutput = null;
                return false;
            }
        }
        //Hasn't been tested
        public static bool Clean(string binPath)
        {
            if (binPath == null || !Directory.Exists(binPath))
            {
                throw new ArgumentException("Either path is null or it does not exist!");
            }
            else
            {
                try
                {
                    DirectoryInfo binInfo = new DirectoryInfo(binPath);
                    FileInfo[] filesInfo = binInfo.GetFiles();
                    for (int index = 0; index < filesInfo.Length; index++)
                    {
                        try
                        {
                            filesInfo[index].Delete();
                            filesInfo[index] = null;
                        }
                        catch
                        {
                            break;
                        }
                    }
                    GC.Collect();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }
    }
}
